﻿/*!
 * 立绘类CharacterImage
 *
 *
 */
var characterimage = function () {
	var ci = $('#characterimage').get(0).getContext('2d'),
		baseline = 600,
		that = {
		// 绘制立绘
		draw: function(src){
			var image = new Image(),
				height,
				width;
			image.src = src;
			height = image.height;
			width = image.width;
			image.onload = function(){
				ci.drawImage(image, 400 - width / 2, 300 - height / 2, width, height);
			}
		},
		// 立绘渐入，由于JS执行的效率问题，实际时间要比time长，越短这个效果越明显
		fadein: function(src, time) {
			var image = new Image(),
				deltaTime = time / 20,
				deltaAlpha = 1 / 20,
				count = 0,
				height,
				width,
				x,
				y;
			image.src = src;
			image.onload = function() {
				height = image.height;
				width = image.width;
				x = 400 - width / 2 > 0 ? 400 - width / 2 : 0;
				y = 300 - height / 2 > 0 ? 300 - height / 2 : 0;
				var timer = setInterval(function(){
					ci.clearRect(0, 0, 800, 600);
					ci.globalAlpha = deltaAlpha * count;
					ci.drawImage(image, 0, 0, 800, 600);
					count++;
					if (count >= 20) {
						clearInterval(timer);
					}
				},deltaTime);
			}
		},
		// 清除立绘
		clear: function() {
			ci.clearRect(0, 0, 800, 600);
		}
	};
	return that;
};